// 路由鉴权
import router from '@/router'
import nprogress from 'nprogress'
import "nprogress/nprogress.css"
import pinia from './store'
import useUserStore from './store/modules/user'
import setting from './setting'
nprogress.configure({showSpinner: false})
let userStore = useUserStore(pinia)
router.beforeEach(async (to: any, from: any, next: any) => {
  document.title = `${setting.title}-${to.meta.title}`
  nprogress.start()
  let token = userStore.user.token
  if (token) {
    if (to.path === '/login') next({path: '/'})
    else {
      if (userStore.user.userName) {
        next()
      } else {
        await userStore.userInfo().then(() => {
          next()
        }).catch(async () => {
          await userStore.userLogOut().then(() => {
            next({path: '/login', query: {redirect: to.path}})
          })
        })
      }
    }
  } else {
    if (to.path === '/login') next()
    else next({path: '/login', query: {redirect: to.path}})
  }
})
router.afterEach((to: any, from: any) => {
  nprogress.done()
})
